package com.ysjweb.dish.Controller;

import com.alibaba.fastjson2.JSON;
import com.ysjweb.dish.Service.impl.AiRecommendByResource;
import com.ysjweb.dish.Service.impl.DishServiceImpl;
import com.ysjweb.dish.pojo.Result;
import com.ysjweb.dish.pojo.UserInformation;
import com.ysjweb.dish.pojo.dish;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.http.MediaType;
import org.springframework.http.server.ServerHttpResponse;
import org.springframework.web.bind.annotation.*;
import reactor.core.publisher.Flux;

import java.time.LocalDate;
import java.util.Map;

@RestController
public class dishController {
    @Autowired
    DishServiceImpl dishService;
    @Autowired
    AiRecommendByResource aiRecommendByResource;

    @GetMapping(value = "/cuisine",produces = "text/html;charset=utf-8")
    public Flux<String> GetCuisine(
            HttpServletResponse response,
            @RequestHeader("userInformation") String userInformationJson,
            @RequestParam String mealTime,
            @RequestParam String constitution,
            @RequestParam String city,
            @RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate mealDate
    ) {
        UserInformation userInformation = JSON.parseObject(userInformationJson, UserInformation.class);
        String weather=dishService.GetUserWeather(city,mealDate);
        String DishName=dishService.GetDishName(weather, userInformation.getId(), mealTime,constitution,city);

        String picture=dishService.GetPicture(DishName);
        String video=dishService.GetVideo(DishName);
        response.addHeader("PictureUrl",picture);
        response.addHeader("VideoUrl",video);
//        if (picture == null) picture = "";
//        if (video == null) video = "";
        return dishService.RecommendDish(DishName,weather,userInformation.getId(),mealTime,constitution, city);
//        return Flux.just(
//                "event: header\ndata: " + JSON.toJSONString(Map.of(
//                        "PictureUrl", picture,
//                        "VideoUrl", video
//                )) + "\n\n",
//                "data: " + dishService.RecommendDish(DishName,weather,userInformation.getId(),mealTime,constitution, city) + "\n\n"
//    );
    }
    @GetMapping("/cuisine/base")
    public Result GetBase(Integer id){
        return Result.success(dishService.GetBase(id));
    }
    @GetMapping("/cuisine/picture")
    public Result GetPicture(Integer id){
        return Result.success(dishService.GetPictureById(id));
    }
    @PutMapping("/cuisine")
    public Result UpdateAI(String suggestion,Integer id) {
        dishService.UpdateAI(suggestion,id);
        return Result.success();
    }
    @PostMapping("/cuisine")
    public Result CreateCuisine(@RequestBody dish newDish,Integer id) {
        return Result.success(dishService.AddCuisine(id,newDish));
    }

    @GetMapping("/cook")
    public Flux<String> GetCook(String need) {
        return aiRecommendByResource.recommendByResource(need);
    }

}
